package com.jxc.repository;

import com.jxc.entity.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 角色Repository接口
 *
 * @author 高旭
 * @date 2021-03-22
 */
public interface RoleRepository extends JpaRepository<Role, Integer> {

    /**
     * 根据用户姓名查询角色集合
     *
     * @param userName
     * @return
     */
    @Query(value = "SELECT r.* FROM `t_user` u, `t_role` r , `t_user_role` ur where r.id = ur.role_id and u.id = ur.user_id and u.user_name = ?1", nativeQuery = true)
    List<Role> findByUserName(String userName);

    /**
     * 根据用户id查角色集合
     *
     * @param id
     * @return
     */
    @Query(value = "SELECT r.* FROM t_user u,t_role r,t_user_role ur WHERE ur.`user_id`=u.`id` AND ur.`role_id`=r.`id` AND u.`id`=?1", nativeQuery = true)
    List<Role> findByUserId(Integer id);
}
